package cn.chia.back.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import cn.chia.back.dao.ISenderDAO;
import cn.chia.pojo.Peisongren;
import cn.chia.utils.Onsender;
@Component
public class SenderDAOImpl extends HibernateDaoSupport implements ISenderDAO {

	@Autowired
	public SenderDAOImpl(HibernateTemplate hibernateTemplate){
		super.setHibernateTemplate(hibernateTemplate);
	}
	
	/*
	 * 于辉  添加配送员
	 */
	public void doCreate(Peisongren vo) throws Exception {
		super.getHibernateTemplate().save(vo);
	}
	
	public void doRemove(Integer id) throws Exception {
		
	}
	
	/*
	 * 于辉  修改配送员
	 */
	public void doUpdate(Peisongren vo) throws Exception {
		super.getHibernateTemplate().update(vo);
	}
	
	/*
	 * 于辉  查询所有的配送员
	 */
	public List<Peisongren> findAll() throws Exception {
		return super.getHibernateTemplate().find("from Peisongren where flag = 1 order by id");
	}

	public List<Peisongren> findAll(int pageNo, int pageSize, String keyword,
			String column) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	/*
	 * 于辉  根据id查配送员
	 */
	public Peisongren findById(Integer id) throws Exception {
		return (Peisongren) super.getHibernateTemplate().find("from Peisongren where id='"+id+"'").get(0);
	}

	public int getAllCount(String keyword, String column) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}
	public List<?> findByUserTel(String psrname, String psrtel) {
		return super.getHibernateTemplate().find("from Peisongren where psrname='"+psrname+"' and psrtel='"+psrtel+"' and flag = 1");
	}

	public List<?> findOrderByStateUser(String state) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        String nowdate = sf.format(c.getTime());
        c.add(Calendar.DAY_OF_MONTH, 1);
        String nowdateaddone = sf.format(c.getTime());
		return super.getHibernateTemplate().find("from Orders where state='"+state+"' and peisongren='"+Onsender.sender().getId()+"' and ordertime.sendtime  between '"+nowdate+"' and '"+nowdateaddone+"'");
	}
}
